package com.efa.backend.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.efa.backend.base.entity.ProductDetail;
import com.efa.backend.base.entity.ProductNews;
import com.efa.backend.base.repository.ProductDetailRepository;
import com.efa.backend.base.repository.ProductNewsRepository;
import com.efa.backend.base.service.ProductNewsService;
import com.efa.backend.framework.utility.DateUtil;

/**
 * @Description: TODO
 * @author zhaoxin
 * @date 2014年3月12日 下午8:05:43
 * @version V1.0
 */

@Service(value = "productNewsService")
public class ProductNewsServiceImpl implements ProductNewsService {
	
	@Autowired
	private ProductDetailRepository productDetailRepository;
	
	@Autowired
	private ProductNewsRepository productNewsRepository;

	@Override
	public List<ProductNews> findByProductDetail(Long id) {
		Pageable page = new PageRequest(0,10,Direction.ASC,"createDate");
		ProductDetail pd = this.productDetailRepository.findOne(id);
		if(pd == null){
			return new ArrayList<ProductNews>();
		}
		List<ProductNews> list = this.productNewsRepository.findByProductDetail(pd, page);
		for (ProductNews pn : list) {
			pn.setCreatedate(DateUtil.dateFormat(pn.getCreateDate(), "yyyy-MM-dd"));
		}
		return list;
	}

}
